package utils

import (
	"fmt"
	"github.com/sirupsen/logrus"
	"gopkg.in/ini.v1"
)

type Config struct {
	Server         *server    `ini:"server"`
	Database       *database  `ini:"database"`
	QiniuyunServer *qNYServer `ini:"qiniuyun"`
}

type server struct {
	AppMode  string `ini:"AppMode"`
	HttpPort string `ini:"HttpPort"`
	JWTKey   string `ini:"JWTKey"`
}

type database struct {
	Db         string `ini:"Db"`
	DbName     string `ini:"DbName"`
	DbUser     string `ini:"DbUser"`
	DbPassWord string `ini:"DbPassWord"`
	DbHost     string `ini:"DbHost"`
	DbPort     string `ini:"DbPort"`
}

type qNYServer struct {
	AccessKey string `ini:"AccessKey"`
	SecretKey string `ini:"SecretKey"`
	Bucket    string `ini:"Bucket"`
	Domain    string `ini:"Domain"`
	Zone      int    `ini:"Zone"`
}

var ServerSetting = &server{
	AppMode:  "debug",
	HttpPort: ":3000",
	JWTKey:   "LuoYu123",
}

var DatabaseSetting = &database{
	Db:         "mysql",
	DbName:     "goblog",
	DbUser:     "root",
	DbPassWord: "ba161754",
	DbHost:     "localhost",
	DbPort:     "3306",
}

var QiniuyunServer = &qNYServer{
	AccessKey: "your_access_key",
	SecretKey: "your_secret_key",
	Bucket:    "your_bucket",
	Domain:    "your_domain",
	Zone:      1,
}

// Config_Message
var Config_Message = &Config{
	Server:         ServerSetting,
	Database:       DatabaseSetting,
	QiniuyunServer: QiniuyunServer,
}

func init() {
	filename := "config/config.ini"
	cfg, err := ini.Load(filename)
	if err != nil {
		logrus.Errorf("配置文件加载失败: %v", err)
	}
	err = cfg.MapTo(Config_Message)
	if err != nil {
		logrus.Errorf("配置文件映射失败: %v", err)
	}
	logrus.Infof("配置文件加载成功")
	fmt.Println(Config_Message.QiniuyunServer.Domain)
}
